﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;
using TjZheng.Power.RedisEntity;
using TjZheng.Quartz;
using TjZheng.Redis;

namespace TjZheng.SyncPowerRedis
{
    public class SyncDependentUserPower : TjZhengJob
    {
        public override void JobExecute(IJobExecutionContext context)
        {
            bool flag = true;
            int pageIndex = 0;
            UserPower objUserPower = null;
            List<UserInfo> objUserInfoList = null;
            List<UserPowerInfo> objUserPowerInfoList = null;
            BizUserInfo objBizUserInfo = new BizUserInfo();
            BizUserPowerInfo objBizUserPowerInfo = new BizUserPowerInfo("Dependent");
            ConsoleHelper.WriteLineYellow(string.Format("[同步用户独立权限][{0}]：开始", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            while (flag)
            {
                objUserInfoList = objBizUserInfo.GetPageList(pageIndex, 10);
                if (objUserInfoList.IsNull())
                {
                    flag = false;
                    ConsoleHelper.WriteLineYellow(string.Format("[同步用户独立权限][{0}]：结束", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    break;
                }
                List<UserPower> objUserPowerList = new List<UserPower>();
                foreach (var objUserInfo in objUserInfoList)
                {
                    objUserPower = new UserPower
                    {
                        UserId = objUserInfo.Id,
                        PowerIds = objBizUserPowerInfo.GetFieldList<long>("UserId=" + objUserInfo.Id, "PowerId")
                    };
                    objUserPowerList.Add(objUserPower);
                }
                if (objUserPowerList.IsNoNull())
                {
                    RedisHelper.StoreAll<UserPower>(objUserPowerList, PowerVariable.ProviderName, PowerVariable.Db, "Dependent");
                }
                pageIndex++;
                ConsoleHelper.WriteLineYellow(string.Format("[同步用户独立权限][{0}]：第{1}页数据同步完成", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), pageIndex));
            }
        }
    }
}
